/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery","formServ","ajaxServ","msgServ","treegridServ","sysmanMain","bizProto","btnServ"], 
	function($,formServ,ajaxServ,msgServ,treegridServ,sysmanMain,bizProto,btnServ) {

	'use strict';

	var _bizProto,$dg,$grid, $edit_window,$form,window_btn,_buttonConfig,_moduleConfig
		
	
	
    var private_method = {
    	//初始化组件对象
    	init:function(doc){
    		_moduleConfig = sysmanMain.getCurrentModuConfig();
    		_bizProto = new bizProto({doc:doc,rootId:'panel-lob',panelSelector:'div.well',subSystemCode:'sysman',moduleCode:'lob'})        		
    		_buttonConfig={
						"新增":private_method.add,
					    "编辑":private_method.edit,
						 "删除":private_method.del,
						 "保存":private_method.submit,
						 "取消":private_method.cancel									
					}	
			private_method.initPath()
			private_method.initElement(_bizProto.pageDoc);
    	},
    	
    	initPath:function(){
    		
    	},
    	initElement:function(doc){
    		if(doc!=null){
    			
    			
    			
    			//grid对象初始化
				$dg = $(doc).find("#lob-dg");
				
				$grid=$dg.treegrid({
					
					
					onDblClickRow:function(row){private_method.edit(false,row)},
					url:_bizProto.treeUrl
				});
				
				
				
				//编辑窗口初始化
				$edit_window = $('#lob-win')
				$form = $($edit_window).find('form')
				
				$($edit_window).find("#parentid").combotree({
					width:171,
					url:_bizProto.treeUrl,
					idFiled:'id',
				 	textFiled:'name',
				 	parentField:'parentid',
				 	onClick:function(node){private_method.setComboValue(node,$($form).find('#parentName'))}
				 	
				});
				
				//初始化操作按钮
				var listTd = $(_bizProto.pageDoc).find('#tb-lob td')
				
				// 编辑窗口初始化
				
				var editTd = $($edit_window).find('#tb-lobEdit div')
				
				var _param = {
					bizcode:_moduleConfig.fullBizcode,
					rootPath:_bizProto.rootPath,
					containers:[listTd,editTd],
					callback:private_method.bindBtnEvent}
				btnServ.generateListAndEditBtn(_param);
			}
			
			
    	},
    	
    	//绑定按钮事件
		bindBtnEvent:function(){
			// 绑定按钮事件
			var btn = $(_bizProto.pageDoc).find('#tb-lob a.function-btn')
			$(btn).unbind('click');
			$(btn).bind('click', private_method.function_btn_listenner)			
			
			window_btn = $($edit_window).find('a.function-btn')
			$(window_btn).unbind('click')
			$(window_btn).bind('click',	private_method.function_btn_listenner)
			
		},
    	
    	setComboValue:function(node,target){
    		if(node!=null){		    			
    			$(target).val(node.text);
    		}
    	},		
		
		//功能按钮监听函数
		function_btn_listenner:function(e){
			
			btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})
			
		},
		//新增函数
		add:function(){
			$form = $($edit_window).find('form')
			$($form).form('clear');
			$edit_window.window('open');
			
		},
		//编辑,flag 为form按钮可见性标志
		edit:function(flag,row){
			
			var _param = {flag:flag,grid:$grid,window:$edit_window,btn:window_btn,nullField:'parentid'}
			treegridServ.edit(_param);			
		},
		
		//删除
		del:function(){
						
			var selectedRecord = $grid.treegrid('getSelected');
			if(selectedRecord){
				treegridServ.delRow({
					url:_bizProto.delUrl,
					data:{id:selectedRecord.id},
					grid:$grid
				})			
			}else{
				msgServ.Msgslide('请选择要删除的记录');
			}
		},
		//提交form
		submit:function(){
			

			var _param = {url:_bizProto.saveUrl,grid:$grid,form:$form}
			treegridServ.save(_param)
		},
		//
		cancel:function(){			
			$($edit_window).window('close');
		}
		
	}
	
	
	
	
	var public_method={
		//销毁方法，释放绑定事件及变量定义
		destory:function(){
			//取消编辑窗口的form按钮绑定
			
			$(window_btn).unbind('click',function(e){private_method.function_btn_listenner(e)})
			
			//取消列表窗口的按钮绑定
			var btn = $(_bizProto.pageDoc).find('a.function-btn')
			$(btn).unbind('click',function(e){private_method.function_btn_listenner(e)})
				
		},
		init:function(doc){
			//初始化页面
			private_method.init(doc);
			if (window.console) {
			console.info('lob modules gets ready and  are loaded.');
			}
		}
	}
		
		
	return public_method;

	

});